package pro.shuangxi.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import mybatisplus.pojo.Users;
import mybatisplus.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pro.shuangxi.auth.utils.JwtUtils;
import pro.shuangxi.common.vo.Result;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class AuthController {
    @Autowired
    IUsersService usersService;

    @RequestMapping("/auth.do")
    @ResponseBody
    public Result auth(String username, String password) {
        Users one = usersService.getOne(
                new QueryWrapper<Users>()
                        .and(e -> {
                            e.eq("username", username)
                             .or()
                             .eq("phonenum", username);
                        })
                        .eq("password", password));
        if (one != null) {
            Map<String, String> info = new HashMap<>();
            info.put("username", one.getUsername());
            info.put("phonenum", one.getPhonenum());
            String token = JwtUtils.generateToken(info);
            info.put("token", token);
            one.setToken(token);
            one.setUpdatetime(new Date());
            usersService.updateById(one);
            return Result.OK(info);
        } else {
            return Result.error("鉴权失败");
        }

    }

}

